import { BaseWindow } from "../../../component/window/BaseWindow";
import { Language } from "../../../../extend/language/Language";
import { NetDispatcher } from "../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../mvc/ServerCommand";
import { VipItemPanel } from "./VipItemPanel";
import { ObjCreate } from "../common/ObjCreate";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { ShopRule } from "../../rules/ShopRule";
import { ShopItemInfo } from "../../resource/info/ShopItemInfo";
import { ShopConfig } from "../../resource/ShopConfig";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { ShopType } from "../../resource/info/ShopType";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { ColorInfo } from "../../resource/info/ColorInfo";
import { BaseItem } from "../common/BaseItem";
import { SPanicBuyItemMsg } from "../../../../Message/Game/SPanicBuyItemMsg";
import { NumInput } from "../common/NumInput";
import { GButton } from "../../../../com/mui/controls/GButton";
import { Alert } from "../../../../com/mui/controls/Alert";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { ColorConfig } from "../../resource/ColorConfig";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ItemData } from "../../resource/info/ItemData";
import { Cache } from "../../cache/Cache";
import { ClockManager } from "../../manager/ClockManager";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { SPlayerItem } from "../../../../Message/Public/SPlayerItem";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class BuyVIPWindow
    
    export  class BuyVIPWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this._vipItemArray = [];
            setSize(423, 236);
            title = Language.getString(41287);
            titleHeight = 26;
            this.init();
            NetDispatcher.addCmdListener(ServerCommand.PanicBuyPlayerCommand, this.onPanicBuyPlayerCommandHandler);
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            return;
        }

        /* internal  */init(): void
        {
            var loc1=null;
            this.vcShopItemInfo = this.getVIPItems();
            var loc2=0;
            while (loc2 < this.vcShopItemInfo.length) 
            {
                loc1 = new VipItemPanel(this.vcShopItemInfo[loc2], loc2);
                loc1.x = 17 + 130 * loc2;
                loc1.y = 37;
                this.addChild(loc1);
                this._vipItemArray.push(loc1);
                ++loc2;
            }
            var loc3=ObjCreate.createTextField("", 22, 206, 360, 20, this);
            loc3.autoSize = flash.text.TextFieldAutoSize.LEFT;
            loc3.htmlText = Language.getString(41282);
            loc3.addEventListener(flash.events.TextEvent.LINK, this.onLinkTextClickHandler);
            return;
        }

        /* internal  */onLinkTextClickHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallShowToTab, ShopRule.ShopMallProp));
            return;
        }

        /* internal  */getVIPItems(): Vector.<ShopItemInfo>
        {
            var loc3=null;
            var loc1=new Vector.<ShopItemInfo>();
            var loc2=ShopConfig.instance.getShopByName(GameDefConfig.instance.getEShopCode(ShopType.Normal));
            var loc4=0;
            var loc5=0;
            while (loc5 < loc2.items.length) 
            {
                if (loc4 >= 3) 
                {
                    break;
                }
                loc3 = loc2.items[loc5];
                if (loc3.itemInfo.item.category == ECategory._ECategoryProp && loc3.itemInfo.item.type == EProp._EPropVIPCard) 
                {
                    ++loc4;
                    loc1.push(loc3);
                }
                ++loc5;
            }
            loc1.sort(this.comparePrice);
            return loc1;
        }

        /* internal  */comparePrice(arg1: ShopItemInfo, arg2: ShopItemInfo): int
        {
            if (arg1.tshopSell.price > arg2.tshopSell.price) 
            {
                return -1;
            }
            if (arg1.tshopSell.price < arg2.tshopSell.price) 
            {
                return 1;
            }
            return 0;
        }

        /* internal  */createSellItem(arg1: ShopItemInfo, arg2: int): Object /* flash.display.Sprite */
        {
            var shopItemInfo: ShopItemInfo;
            var index: int;
            var sp: Object /* flash.display.Sprite */;
            var textFormatCenter: Object /* flash.text.TextFormat */;
            var tf: Object /* flash.text.TextField */;
            var colorInfo: ColorInfo;
            var propBg: Object /* flash.display.Bitmap */;
            var propItem: BaseItem;
            var tfPrice: Object /* flash.text.TextField */;
            var price: int;
            var offer: int;
            var activeOffer: int;
            var panicBuyItem: SPanicBuyItemMsg;
            var serverOpenDay: int;
            var panicBuyDict: Map<any, any> /* flash.utils.Dictionary */;
            var buyAmount: int;
            var bmpPrice: Object /* flash.display.Bitmap */;
            var numInput: NumInput;
            var btnBuyAndUse: GButton;
            var onBuyAndUse: Function;
            var onClose: Function;

            var loc1;
            sp = null;
            colorInfo = null;
            price = 0;
            panicBuyItem = null;
            serverOpenDay = 0;
            buyAmount = 0;
            numInput = null;
            onBuyAndUse = null;
            onClose = null;
            shopItemInfo = arg1;
            index = arg2;
            onBuyAndUse = (arg1: Object /* flash.events.MouseEvent */): void
            {
                var loc1=Language.getStringByParam(41285, numInput.currentNum.toString(), colorInfo.color, shopItemInfo.itemInfo.item.name, (numInput.currentNum * price).toString());
                Alert.show(loc1, null, Alert.OK | Alert.CANCEL, null, onClose);
                return;
            }
            onClose = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItemAndUse, {"shopItemInfo": shopItemInfo, "amount": numInput.currentNum}));
                    Dispatcher.addEventListener(EventName.ShopMallBuyItemSuccess, onBuySuccess);
                }
                return;
            }
            sp = new Object /* flash.display.Sprite */();
            sp.mouseEnabled = false;
            ObjCreate.createBg(0, 0, 128, 163, sp);
            textFormatCenter = new GTextFormat(FontUtil.songtiName, 12, 11661311);
            textFormatCenter.align = flash.text.TextFormatAlign.CENTER;
            tf = ObjCreate.createTextField("", 32, 6, 65, 20, sp, textFormatCenter);
            colorInfo = ColorConfig.instance.getItemColor(shopItemInfo.itemInfo.item.color);
            tf.htmlText = HTMLUtil.addColor(shopItemInfo.itemInfo.item.name, colorInfo.color);
            propBg = GlobalClass.getBitmap("PropBg");
            propBg.x = 30;
            propBg.y = 22;
            sp.addChild(propBg);
            propItem = new BaseItem();
            propItem.isDragAble = false;
            propItem.label = "";
            propItem.move(35, 27);
            propItem.itemData = new ItemData(shopItemInfo.itemInfo.item.code);
            propItem.drawNow();
            propItem.amount = -1;
            propItem.setSize(60, 60);
            sp.addChild(propItem);
            tfPrice = ObjCreate.createTextField("", 18, 92, 66, 20, sp);
            price = shopItemInfo.tshopSell.price;
            offer = shopItemInfo.tshopSell.offer;
            activeOffer = shopItemInfo.tshopSell.activeOffer;
            panicBuyDict = Cache.instance.shop.panicBuyDict;
            if (Cache.instance.shop.isShopMallDiscount && activeOffer > 0) 
            {
                price = activeOffer;
            }
            else if (offer > 0) 
            {
                price = offer;
            }
            if (ClockManager.instance.serverOpenDateNum <= 3) 
            {
                panicBuyItem = Cache.instance.shop.getPanicShopItem(shopItemInfo);
                if (panicBuyItem && panicBuyDict[panicBuyItem.itemCode]) 
                {
                    buyAmount = panicBuyDict[panicBuyItem.itemCode];
                }
                if (panicBuyItem && panicBuyItem.buyLimit - buyAmount > 0) 
                {
                    price = panicBuyItem.discount;
                }
            }
            tfPrice.htmlText = Language.getString(41283) + HTMLUtil.addColor(price.toString(), GlobalStyle.color6);
            bmpPrice = GlobalClass.getBitmap(GameDefConfig.instance.getEPrictUnitImg(shopItemInfo.unit));
            bmpPrice.x = 80;
            bmpPrice.y = 92;
            sp.addChild(bmpPrice);
            numInput = new NumInput();
            numInput.x = 15;
            numInput.y = 110;
            numInput.maxNum = 99;
            numInput.currentNum = 1;
            sp.addChild(numInput);
            if (index != 0) 
            {
                numInput.maxNum = 99;
                numInput.currentNum = 1;
            }
            else 
            {
                numInput.maxNum = 1;
            }
            btnBuyAndUse = ObjCreate.createGButton(Language.getString(41284), 24, 133, 80, 26, sp, "ButtonLarge");
            btnBuyAndUse.addEventListener(flash.events.MouseEvent.CLICK, onBuyAndUse);
            return sp;
        }

        /* internal  */onBuySuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1["itemCode"];
            var loc3=loc1["amount"];
            var loc4;
            (loc4 = new SPlayerItem()).itemCode = loc2;
            var loc5=new ItemData(loc4);
            if (ItemsUtil.isVIPCard(loc5)) 
            {
                this.hide();
                Dispatcher.removeEventListener(EventName.ShopMallBuyItemSuccess, this.onBuySuccess);
            }
            return;
        }

        /* internal  */onPanicBuyPlayerCommandHandler(arg1: Map<any, any> /* flash.utils.Dictionary */): void
        {
            if (this && this.isHide == false) 
            {
                this.resetItems();
            }
            return;
        }

        /* internal  */resetItems(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            while (loc3 < this._vipItemArray.length) 
            {
                loc1 = this._vipItemArray[loc3] as VipItemPanel;
                loc2 = this.vcShopItemInfo[loc3] as ShopItemInfo;
                loc1.updateByShopItem(loc2);
                ++loc3;
            }
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show();
            this.resetItems();
            return;
        }

        public static get instance(): BuyVIPWindow
        {
            if (!BuyVIPWindow._instance) 
            {
                BuyVIPWindow._instance = new BuyVIPWindow();
            }
            return BuyVIPWindow._instance;
        }

        /* internal */ /* var */vcShopItemInfo: Vector.<ShopItemInfo>;

        /* internal */ /* var */_vipItemArray: Array<any>;

        /* internal */ static /* var */_instance: BuyVIPWindow;
    }
